<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin\Sys;

use App\Controller\AbstractController;
use App\Middleware\AuthMiddleware;
use App\Model\Feedback;
use App\Model\Message;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;

#[Middlewares([AuthMiddleware::class])]
#[Controller]
class FeedbackController extends AbstractController
{
    #[GetMapping(path: '/sys/feedback/list')]
    public function list()
    {
        $pageNum = $this->request->input('pageNum', 1);
        $pageSize = $this->request->input('pageSize', 10);
        $status = $this->request->input('status', '');
        $type = $this->request->input('type', '');
        $where = [];
        if ($status != '') {
            $where[] = ['status', '=', $status];
        }
        if ($type) {
            $where[] = ['type', '=', $type];
        }

        $list = Feedback::query()
            ->select('id', 'user_id','content', 'type', 'reply_content as replyContent', 'status', 'create_time as createTime', 'update_time as updateTime')
            ->when($where, function ($query) use ($where) {
                return $query->where($where);
            })
            ->with(['user'])
            ->forPage($pageNum, $pageSize)->get();
        $data['searchCount'] = true;
        $data['current'] = (int)$pageNum;
        $data['size'] = (int)$pageSize;
        $data['total'] = Message::query()->count();
        $data['pages'] = ceil($data['total'] / $pageSize);
        foreach ($list as $item){
            $item['avatar'] = $item['user']['avatar'] ?? '';
            $item['nickname'] = $item['user']['nickname'] ?? '';
        }
        $data['records'] = $list;
        return success($data);
    }


    #[DeleteMapping(path: '/sys/feedback/delete/{ids}')]
    public function delete($ids)
    {
        $idArr = explode(',', $ids);
        Feedback::query()->whereIn('id', $idArr)->delete();
        return success();
    }

    #[PutMapping(path: '/sys/feedback/update')]
    public function update()
    {
        $params = $this->request->all();
        Feedback::query()->where('id', $params['id'])->update([
            'content' => $params['content'],
            'reply_content' => $params['replyContent'],
            'status' => $params['status']
        ]);
        return success();
    }

    #[PostMapping(path: '/sys/feedback/add')]
    public function add()
    {
        $user_id = Context::get('uid');
        $params = $this->request->all();
        Feedback::query()->create([
            'user_id' => $user_id,
            'content' => $params['content'],
            'email' => $params['email'] ?? '',
            'type' => $params['type']
        ]);
        return success();
    }
}
